﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.aswingui {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import Display.*;
    import flash.text.*;
    import org.aswing.border.*;
    import L10n.*;

    public class ASwingHelper {

        private static var _sbl:SoftBoxLayout;
        private static var _vbl:SoftBoxLayout;
        private static var _dlb:DecorateBorder;
        private static var cache:Dictionary = new Dictionary(true);
        public static var _debugOff:Boolean = true;

        public static function get debugOff():Boolean{
            return (false);
        }
        public static function setDebug(_arg1):void{
            _arg1.setBorder(new EmptyBorder(ASwingHelper.debugLB));
        }
        public static function setDebugAll(_arg1):void{
            var _local2:int;
            setDebug(_arg1);
            if (_arg1.hasOwnProperty("getComponentCount")){
                _local2 = 0;
                while (_local2 < _arg1.getComponentCount()) {
                    if (_arg1.getComponentCount() > 0){
                        ASwingHelper.setDebugAll(_arg1.getComponent(_local2));
                    } else {
                        setDebug(_arg1);
                    };
                    _local2++;
                };
            };
        }
        public static function makeLabel(_arg1:String, _arg2:String, _arg3:int, _arg4:uint=0xFFFFFF, _arg5:int=0):JLabel{
            var _local6:JLabel = new JLabel(_arg1, null, _arg5);
            _local6.setFont(new ASFont(_arg2, _arg3, true, false, false, new ASFontAdvProperties(true, AntiAliasType.ADVANCED, GridFitType.PIXEL)));
            _local6.setForeground(new ASColor(_arg4));
            ASwingHelper.prepare(_local6);
            return (_local6);
        }
        public static function makeTextField(_arg1:String, _arg2:String, _arg3:int, _arg4:uint=0xFFFFFF, _arg5:Number=0, _arg6:TextFormat=null):JTextField{
            var _local8:int;
            var _local9:TextFormat;
            var _local7:JTextField = new JTextField(_arg1);
            _local7.setEditable(false);
            if (Global.localizer.langCode != "en"){
                if (_arg3 >= 20){
                    _local8 = int((_arg3 * 0.85));
                } else {
                    _local8 = _arg3;
                };
            } else {
                _local8 = _arg3;
            };
            _local7.setFont(new ASFont(_arg2, _arg3, true, false, false, new ASFontAdvProperties(true, AntiAliasType.ADVANCED, GridFitType.PIXEL)));
            _local7.setForeground(new ASColor(_arg4));
            _local7.setBackgroundDecorator(null);
            _local7.setBackground(null);
            _local7.getTextField().selectable = false;
            if (_arg6 != null){
                _local7.setTextFormat(_arg6);
            } else {
                _local9 = _local7.getTextFormat();
                _local9.kerning = true;
                _local9.letterSpacing = _arg5;
                _local7.setTextFormat(_local9);
            };
            ASwingHelper.prepare(_local7);
            return (_local7);
        }
        public static function getStandardFont(_arg1:int):ASFont{
            var _local2:ASFont = new ASFont(EmbeddedArt.DEFAULT_FONT_NAME, _arg1, false, false, false, new ASFontAdvProperties(true, AntiAliasType.ADVANCED, GridFitType.PIXEL));
            return (_local2);
        }
        public static function getBoldFont(_arg1:int):ASFont{
            var _local2:ASFont = new ASFont(EmbeddedArt.DEFAULT_FONT_NAME_BOLD, _arg1, false, false, false, new ASFontAdvProperties(true, AntiAliasType.ADVANCED, GridFitType.PIXEL));
            return (_local2);
        }
        public static function centerElement(_arg1:Component):JPanel{
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local2.append(_arg1);
            return (_local2);
        }
        public static function leftAlignElement(_arg1:Component):JPanel{
            var _local2:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            _local2.append(_arg1);
            return (_local2);
        }
        public static function rightAlignElement(_arg1:Component):JPanel{
            var _local2:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.RIGHT);
            _local2.append(_arg1);
            return (_local2);
        }
        public static function bottomAlignElement(_arg1:Component):JPanel{
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.BOTTOM);
            _local2.append(_arg1);
            return (_local2);
        }
        public static function topAlignElement(_arg1:Component):JPanel{
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            _local2.append(_arg1);
            return (_local2);
        }
        public static function getTitleFont(_arg1:int):ASFont{
            var _local2:ASFont = new ASFont(EmbeddedArt.TITLE_FONT, _arg1, false, false, false, new ASFontAdvProperties(true, AntiAliasType.ADVANCED, GridFitType.PIXEL));
            return (_local2);
        }
        public static function setEasyBorder(_arg1:Component, _arg2:Number, _arg3:Number=0, _arg4:Number=0, _arg5:Number=0):void{
            _arg1.setBorder(new EmptyBorder(null, new Insets(_arg2, _arg3, _arg4, _arg5)));
        }
        public static function setBackground(_arg1:Component, _arg2:DisplayObject):void{
            _arg1.setBackgroundDecorator(new AssetBackground(_arg2));
        }
        public static function setSizedBackground(_arg1:JPanel, _arg2:DisplayObject, _arg3:Insets=null):void{
            var _local4:Number;
            var _local5:Number;
            if (_arg3 == null){
                _arg1.setBackgroundDecorator(new AssetBackground(_arg2));
                _arg1.setPreferredSize(new IntDimension(_arg2.width, _arg2.height));
                _arg1.setMaximumSize(new IntDimension(_arg2.width, _arg2.height));
                _arg1.setMinimumSize(new IntDimension(_arg2.width, _arg2.height));
            } else {
                _local4 = ((_arg3.left + _arg3.right) + _arg2.width);
                _local5 = ((_arg3.top + _arg3.bottom) + _arg2.height);
                _arg1.setBackgroundDecorator(new MarginBackground(_arg2, _arg3));
                _arg1.setPreferredSize(new IntDimension(_local4, _local5));
                _arg1.setMaximumSize(new IntDimension(_local4, _local5));
                _arg1.setMinimumSize(new IntDimension(_local4, _local5));
            };
        }
        public static function makeMarketCloseButton():JButton{
            var _local1:DisplayObject = (new EmbeddedArt.mkt_close_up() as DisplayObject);
            var _local2:DisplayObject = (new EmbeddedArt.mkt_close_over() as DisplayObject);
            var _local3:DisplayObject = (new EmbeddedArt.mkt_close_down() as DisplayObject);
            var _local4:SimpleButton = new SimpleButton(_local1, _local2, _local3, _local1);
            var _local5:JButton = new JButton();
            _local5.wrapSimpleButton(_local4);
            return (_local5);
        }
        public static function makeLittleCloseButton():JButton{
            var _local1:DisplayObject = (new EmbeddedArt.littleClose_up() as DisplayObject);
            var _local2:DisplayObject = (new EmbeddedArt.littleClose_over() as DisplayObject);
            var _local3:DisplayObject = (new EmbeddedArt.littleClose_down() as DisplayObject);
            var _local4:SimpleButton = new SimpleButton(_local1, _local2, _local3, _local1);
            var _local5:JButton = new JButton();
            _local5.wrapSimpleButton(_local4);
            return (_local5);
        }
        public static function makeCloseButtonOffset():JPanel{
            var _local1:DisplayObject = (new EmbeddedArt.mkt_close_up() as DisplayObject);
            var _local2:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            _local2.append(ASwingHelper.horizontalStrut(_local1.width));
            return (_local2);
        }
        public static function makeCloseButton():JButton{
            var _local1:DisplayObject = (new EmbeddedArt.dia_close_on() as DisplayObject);
            var _local2:DisplayObject = (new EmbeddedArt.dia_close_over() as DisplayObject);
            var _local3:SimpleButton = new SimpleButton(_local1, _local2, _local1, _local1);
            var _local4:JButton = new JButton();
            _local4.wrapSimpleButton(_local3);
            return (_local4);
        }
        public static function getColor(_arg1:uint):ASColor{
            var _local2:ASColor = new ASColor(_arg1, 1);
            return (_local2);
        }
        public static function getWhite():ASColor{
            var _local1:ASColor = new ASColor(0xFFFFFF, 1);
            return (_local1);
        }
        public static function makeMultilineText(_arg1:String, _arg2:int, _arg3:String, _arg4:String, _arg5:int=12, _arg6:uint=0, _arg7:Array=null, _arg8:Boolean=false):AssetPane{
            var _local9:TextField = new TextField();
            if (_arg2 == -1){
                _local9.multiline = false;
                _local9.wordWrap = false;
                _local9.text = _arg1;
                _local9.width = _local9.textWidth;
            } else {
                _local9.width = _arg2;
                _local9.multiline = true;
                _local9.wordWrap = true;
                _local9.text = _arg1;
            };
            _local9.height = 0;
            _local9.selectable = false;
            _local9.embedFonts = true;
            _local9.antiAliasType = AntiAliasType.ADVANCED;
            _local9.gridFitType = GridFitType.PIXEL;
            if (_arg4 == TextFormatAlign.LEFT){
                _local9.autoSize = TextFieldAutoSize.LEFT;
            } else {
                _local9.autoSize = TextFieldAutoSize.CENTER;
            };
            var _local10:TextFormat = new TextFormat();
            _local10.font = _arg3;
            _local10.color = _arg6;
            _local10.align = _arg4;
            _local10.size = _arg5;
            if (_arg8){
                _local9.defaultTextFormat = _local10;
                _local9.htmlText = _arg1;
            } else {
                _local9.defaultTextFormat = _local10;
                _local9.setTextFormat(_local10);
            };
            _local9.filters = _arg7;
            var _local11:AssetPane = new AssetPane(_local9);
            _local11.setPreferredWidth((_arg2 + 15));
            ASwingHelper.prepare(_local11);
            return (_local11);
        }
        public static function makeMultilineCapsText(_arg1:String, _arg2:int, _arg3:String, _arg4:String, _arg5:int=12, _arg6:uint=0, _arg7:Array=null, _arg8:Boolean=false, _arg9:int=0):AssetPane{
            var _local10:TextField = new TextField();
            if (_arg2 == -1){
                _local10.multiline = false;
                _local10.wordWrap = false;
                _local10.text = _arg1;
                _local10.width = _local10.textWidth;
            } else {
                _local10.width = _arg2;
                _local10.multiline = true;
                _local10.wordWrap = true;
                _local10.text = _arg1;
            };
            if (_arg8 == true){
                _local10.htmlText = _arg1;
            };
            _local10.height = _arg9;
            if (_arg9 == 0){
                _local10.autoSize = TextFieldAutoSize.CENTER;
            };
            _local10.selectable = false;
            _local10.embedFonts = true;
            _local10.antiAliasType = AntiAliasType.ADVANCED;
            _local10.gridFitType = GridFitType.SUBPIXEL;
            var _local11:TextFormat = new TextFormat();
            _local11.font = _arg3;
            _local11.color = _arg6;
            _local11.align = _arg4;
            _local11.size = _arg5;
            _local11.leading = (-(_arg5) / 4);
            _local10.filters = _arg7;
            TextFieldUtil.formatSmallCaps(_local10, new TextFormat(_arg3, (_arg5 + 4)));
            _local10.setTextFormat(_local11);
            LocaleHelper.localSetting.TextField_adjust(_local10, {
                bold:true,
                leading:0
            });
            var _local12:AssetPane = new AssetPane(_local10);
            ASwingHelper.prepare(_local12);
            return (_local12);
        }
        public static function makeFont(_arg1:String, _arg2:int):ASFont{
            var _local3:String = Fonts.ensureFont(_arg1);
            if (_local3){
                return (new ASFont(_local3, _arg2, false, false, false, new ASFontAdvProperties(true, AntiAliasType.ADVANCED, GridFitType.PIXEL)));
            };
            return (new ASFont(_arg1, _arg2, false, false, false, new ASFontAdvProperties(false, AntiAliasType.ADVANCED, GridFitType.PIXEL)));
        }
        public static function prepare(... _args):void{
            var _local2:int;
            var _local3:Component;
            if (_args){
                _local2 = 0;
                while (_local2 < _args.length) {
                    _local3 = (_args[_local2] as Component);
                    if (_local3){
                        _local3.invalidatePreferSizeCaches();
                        _local3.pack();
                        _local3.revalidate();
                    };
                    _local2++;
                };
            };
        }
        public static function makeFlowJPanel(_arg1:int=1, _arg2:int=0, _arg3:int=0):JPanel{
            var _local4:JPanel = new JPanel(new FlowLayout(_arg1, _arg2, _arg3, false));
            return (_local4);
        }
        public static function makeSoftBoxJPanel(_arg1:int=1, _arg2:int=0):JPanel{
            var _local3:JPanel = new JPanel(new SoftBoxLayout(SoftBoxLayout.X_AXIS, _arg2, _arg1));
            return (_local3);
        }
        public static function makeSoftBoxJPanelVertical(_arg1:int=1, _arg2:int=0):JPanel{
            var _local3:JPanel = new JPanel(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, _arg2, _arg1));
            return (_local3);
        }
        public static function get softBoxLayout():SoftBoxLayout{
            if (!_sbl){
                _sbl = new SoftBoxLayout(SoftBoxLayout.X_AXIS, 1);
            };
            return (_sbl);
        }
        public static function get softBoxLayoutVertical():SoftBoxLayout{
            if (!_vbl){
                _vbl = new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 1);
            };
            return (_vbl);
        }
        public static function get debugLB():DecorateBorder{
            if (debugOff){
                return (null);
            };
            _dlb = new LineBorder(null, new ASColor((0xFFFFFF * Math.random())), 0.5, 0);
            return (_dlb);
        }
        public static function debug(_arg1:String):DecorateBorder{
            return (debugLB);
        }
        public static function get debugOnce():DecorateBorder{
            return (debugLB);
        }
        public static function verticalStrut(_arg1:int, _arg2:Boolean=false):JPanel{
            var _local3:JPanel = new JPanel();
            _local3.setPreferredSize(new IntDimension(1, _arg1));
            if (_arg2 == true){
                _local3.setBorder(debugLB);
            };
            return (_local3);
        }
        public static function horizontalStrut(_arg1:int, _arg2:Boolean=false):JPanel{
            var _local3:JPanel = new JPanel();
            _local3.setPreferredSize(new IntDimension(_arg1, 1));
            if (_arg2 == true){
                _local3.setBorder(debugLB);
            };
            return (_local3);
        }
        public static function setTextSpacing(_arg1:DisplayObjectContainer, _arg2:Number, _arg3:int=-1):Boolean{
            var _local7:*;
            var _local8:TextFormat;
            var _local4:Boolean;
            var _local5:int = _arg1.numChildren;
            var _local6:int;
            while (_local6 < _local5) {
                _local7 = _arg1.getChildAt(_local6);
                if ((_local7 is TextField)){
                    _local8 = (_local7 as TextField).getTextFormat();
                    _local8.kerning = true;
                    _local8.underline = true;
                    _local8.letterSpacing = _arg2;
                    (_local7 as TextField).text = "blah";
                } else {
                    if ((_local7 is DisplayObjectContainer)){
                        if (_arg3 < 1000){
                            _local4 = setTextSpacing(_local7, _arg2, (_arg3 + 1));
                        } else {
                            return (false);
                        };
                    };
                };
                if (_local4){
                    return (true);
                };
                _local6++;
            };
            return (false);
        }
        public static function makeDisabledFilter():ColorMatrixFilter{
            var _local1:Array = [0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0, 0, 0, 1, 0];
            return (new ColorMatrixFilter(_local1));
        }
        public static function getTokenIndices(_arg1:String, _arg2:String, _arg3:int):Array{
            if (!_arg3){
                return (null);
            };
            var _local4:Array = new Array();
            var _local5:int;
            var _local6:int;
            var _local7:int;
            _local5 = 0;
            while (_local5 < _arg3) {
                _local4[_local5] = new Array();
                _local4[_local5][0] = (_arg1.indexOf("{", _local6) - _local7);
                _local4[_local5][1] = _arg2.indexOf(" ", _local4[_local5][0]);
                if (_local4[_local5][1] == -1){
                    _local4[_local5][1] = (_arg2.length - 1);
                };
                _local6 = _local4[_local5][1];
                _local7 = (_local4[_local5][1] - _local4[_local5][0]);
                _local5++;
            };
            return (_local4);
        }
        public static function multicolorize(_arg1:JTextField, _arg2:Array, _arg3:Array):JTextField{
            var _local7:uint;
            var _local8:int;
            var _local4:JTextField = _arg1;
            var _local5:String = _local4.getText();
            var _local6:TextFormat = new TextFormat();
            for each (_local7 in _arg3) {
                _local6.color = _local7;
            };
            _local8 = 0;
            while (_local8 < _arg3.length) {
                _local6.color = _arg3[_local8];
                _local4.setTextFormat(_local6, _arg2[_local8][0], _arg2[_local8][1]);
                _local8++;
            };
            return (_local4);
        }
        public static function colorize(_arg1:String, _arg2:Number):String{
            var _local3 = (((("<font color='#" + _arg2.toString(16)) + "'>") + _arg1) + "</font>");
            return (_local3);
        }
        public static function applyFont(_arg1:String, _arg2:String):String{
            var _local3 = (((("<font face='" + _arg2) + "'>") + _arg1) + "</font>");
            return (_local3);
        }
        public static function setForcedSize(_arg1:Component, _arg2:IntDimension):void{
            _arg1.setPreferredSize(_arg2);
            _arg1.setMinimumSize(_arg2);
            _arg1.setMaximumSize(_arg2);
        }
        public static function setForcedWidth(_arg1:Component, _arg2:int):void{
            _arg1.setPreferredWidth(_arg2);
            _arg1.setMinimumWidth(_arg2);
            _arg1.setMaximumWidth(_arg2);
        }
        public static function setForcedHeight(_arg1:Component, _arg2:int):void{
            _arg1.setPreferredHeight(_arg2);
            _arg1.setMinimumHeight(_arg2);
            _arg1.setMaximumHeight(_arg2);
        }
        public static function scaleToFit(_arg1:DisplayObject, _arg2:Component):Number{
            var _local3:Number = 1;
            var _local4:Number = 1;
            var _local5:int = _arg2.getPreferredHeight();
            var _local6:int = _arg2.getPreferredWidth();
            if (_arg1.width > _local6){
                _local3 = (_local6 / _arg1.width);
            };
            if (_arg1.height > _local5){
                _local4 = (_local5 / _arg1.height);
            };
            return (Math.min(_local3, _local4));
        }
        public static function makeFriendImageFromZid(_arg1:String):JPanel{
            var asset:* = null;
            var image:* = null;
            var url:* = null;
            var zid:* = _arg1;
            var container:* = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            asset = (new EmbeddedArt.hud_no_profile_pic() as DisplayObject);
            image = new Sprite();
            if (zid.substr(0, 1) == "-"){
                url = Global.player.findFriendById("-1").snUser.picture;
            } else {
                if (zid == Global.player.uid){
                    url = Global.player.snUser.picture;
                } else {
                    if (Global.player.findFriendById(zid)){
                        url = Global.player.findFriendById(zid).snUser.picture;
                    };
                };
            };
            image.addChild(asset);
            var imageContainer:* = new AssetPane(image);
            ASwingHelper.setEasyBorder(imageContainer, 0, 0, 0, 0);
            ASwingHelper.prepare(imageContainer);
            if (url){
                LoadingManager.loadFromUrl(url, {
                    priority:LoadingManager.PRIORITY_HIGH,
                    completeCallback:function (_arg1:Event):void{
                        image.removeChild(asset);
                        asset = (_arg1.target.content as DisplayObject);
                        image.addChild(asset);
                    }
                });
            };
            container.append(imageContainer);
            ASwingHelper.prepare(container);
            return (container);
        }

    }
}//package Display.aswingui 
